﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Toth.WLW.Plugins
{
	public partial class formPingFMAPIKey : Form
	{
		public formPingFMAPIKey()
		{
			InitializeComponent();
		}

		public formPingFMAPIKey(string pingFMAPIKey)
			: this()
		{
			textBoxPingFMAPIKey.Text = pingFMAPIKey;
		}

		private void formPingFMAPIKey_FormClosing(object sender, FormClosingEventArgs e)
		{
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(textBoxPingFMAPIKey.Text))
			{
				// Validate the API key
				PingFM.PingFMApi api = new PingFM.PingFMApi(textBoxPingFMAPIKey.Text);

				PingFM.PingFMApi.PingResponse validateResponse = api.Validate();

				if (validateResponse == null)
				{
					MessageBox.Show(this, "Could not connect to Ping.FM api services. Please check your internet connection.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}

				if (string.Equals(validateResponse.Status, "OK", StringComparison.InvariantCultureIgnoreCase))
				{
					DialogResult = DialogResult.OK;
				}
				else
				{
					// Invalid, show an error
					MessageBox.Show(this, "Could not validate your API key. Please try another key.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
				} 
			}
			else
			{
				DialogResult = DialogResult.OK;
			}

		}



	}
}
